<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
pc_base::load_sys_class('form', '', 0);
class project extends admin {
	private $db, $db_usr;
	function __construct() {
		parent::__construct();
		$this->db = pc_base::load_model('project_model');
		$this->db_usr = pc_base::load_model('project_usr_model');
	}
	
	public function init() {
			$infos = array();
			$status = array(0=>'解散',1=>'正常');
			
			//部门
			$deptlist = array();
			$deptdb = pc_base::load_model('dept_model');
			$depres = $deptdb->select("","id,name");
			if($depres)foreach($depres as $v){
				$deptlist[$v['id']] = $v['name'];
			}
			
			$where = "";
			$page = $_GET['page'] ? $_GET['page'] : '1';
			$infos = $this->db->listinfo($where, $order = 'listorder ASC,id ASC', $page, $pagesize = 20);
			$pages = $this->db->pages;
			$show_dialog = true;
			$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=admin&c=project&a=add\', title:\'添加项目\', width:\'500\', height:\'300\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', '添加项目');
 			include $this->admin_tpl('project_list');
	}
	
	/**
	 * 推荐位添加
	 */
	public function add() {
		if(isset($_POST['dosubmit'])) {
			if(!is_array($_POST['info']) || empty($_POST['info']['name'])){
				showmessage(L('operation_failure'));
			}
			$_POST['info']['deptid'] = intval($_POST['info']['deptid']);
			$_POST['info']['uid'] = intval($_POST['info']['uid']);
			$_POST['info']['status'] = intval($_POST['info']['status']);
			$_POST['info']['memo'] = trim($_POST['info']['memo']);
			$info = $_POST['info'];
			$info['created'] = $info['updated'] = time();
			$insert_id = $this->db->insert($info,true);
			//保存人员
			
			//$this->_set_cache();
			if($insert_id){
				showmessage(L('operation_success'), '', '', 'add');
			}
		} else {
			pc_base::load_sys_class('form');
				
			$deptlist = array();
			$deptdb = pc_base::load_model('dept_model');
			$deptlist = $deptdb->select("","id,name");
			
			$show_header = $show_validator = true;
			include $this->admin_tpl('project_add');
		}
		
	}
	
	/**
	 * 推荐位编辑
	 */
	public function edit() {
		if(isset($_POST['dosubmit'])) {
			$_POST['id'] = intval($_POST['id']);
			if(!is_array($_POST['info']) || empty($_POST['info']['name'])){
				showmessage(L('operation_failure'));
			}
			$info = array();
			$_POST['info']['deptid'] = intval($_POST['info']['deptid']);
			$_POST['info']['memo'] = trim($_POST['info']['memo']);		
			$_POST['info']['uid'] = intval($_POST['info']['uid']);
			$_POST['info']['status'] = intval($_POST['info']['status']);
			$_POST['info']['memo'] = trim($_POST['info']['memo']);
			$info = $_POST['info'];
			$info['updated'] = time();
			$this->db->update($info,array('id'=>$_POST['id']));
			//保存人员
						
			//$this->_set_cache();
			showmessage(L('operation_success'), '', '', 'edit');
		} else {
			$id = intval($_POST['id']);
			$info = $this->db->get_one(array('id'=>intval($_GET['id'])));
		
			pc_base::load_sys_class('form');
			
			$deptlist = array();
			$deptdb = pc_base::load_model('dept_model');
			$deptlist = $deptdb->select("","id,name");
			
			$show_validator = $show_header = $show_scroll = true;
			include $this->admin_tpl('project_edit');
		}

	}
	
	/**
	 * 推荐位删除
	 */
	public function delete() {
		$id = intval($_GET['id']);
		$this->db->delete(array('id'=>$id));
		//$this->_set_cache();
		showmessage(L('posid_del_success'),'?m=admin&c=project');
	}
	
	/**
	 * 推荐位排序
	 */
	public function listorder() {
		if(isset($_POST['dosubmit'])) {
			foreach($_POST['listorders'] as $id => $listorder) {
				$this->db->update(array('listorder'=>$listorder),array('id'=>$id));
			}
			//$this->_set_cache();
			showmessage(L('operation_success'),'?m=admin&c=project');
		} else {
			showmessage(L('operation_failure'),'?m=admin&c=project');
		}
	}
}
?>